//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.util;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class TextFile extends ArrayList {
    public static String read(String fileName) throws IOException {
        File file = new File(fileName);
        return read(file);
    }

    public static String read(File fileName) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedInputStream bis = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(fileName));
            byte[] buffer = new byte[1024];
            boolean var4 = false;

            while(true) {
                int len = bis.read(buffer, 0, buffer.length);
                if (len == -1) {
                    return sb.toString();
                }

                sb.append(new String(buffer, 0, len));
            }
        } finally {
            if (bis != null) {
                bis.close();
            }

        }
    }

    public static void write(String fileName, String text) throws IOException {
        File file = new File(fileName);
        write(file, text);
    }

    public static void write(File file, String text) throws IOException {
        write(file, text, false);
    }

    public static void write(File file, String text, boolean append) throws IOException {
        PrintWriter out = null;

        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(file, append)));
            out.print(text);
        } finally {
            if (out != null) {
                out.close();
            }

        }

    }

    public TextFile(String fileName) throws IOException {
        super(Arrays.asList(read(fileName).split("\n")));
    }

    public void write(String fileName) throws IOException {
        PrintWriter out = null;

        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));

            for(int i = 0; i < this.size(); ++i) {
                out.println(this.get(i));
            }
        } finally {
            if (out != null) {
                out.close();
            }

        }

    }

    public static String read(String fileName, Object parent) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        BufferedInputStream bis = null;

        try {
            is = parent.getClass().getResourceAsStream(fileName);
            bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            boolean var6 = false;

            while(true) {
                int len = bis.read(buffer, 0, buffer.length);
                if (len == -1) {
                    return sb.toString();
                }

                sb.append(new String(buffer, 0, len));
            }
        } finally {
            if (bis != null) {
                bis.close();
            }

            if (is != null) {
                is.close();
            }

        }
    }
}
